﻿<%@ WebHandler Language="C#" Class="SysGroupAuthorizationHandler" %>

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;

using Web.Library.DomainObject.SQLServer;
using Web.Library.Facade.SQLServer;

using Web.Support;
public class SysGroupAuthorizationHandler : BaseHandler {
    private SysGroupAuthorizationFacade Facade = new SysGroupAuthorizationFacade(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);

    private SysMenuFacade FacadeMenu = new SysMenuFacade(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);

    public override void ProcessRequest(HttpContext context)
    {
        base.ProcessRequest(context);
        context.Response.ContentType = "text/json";
        string result = this.Process(context);
        context.Response.Write(result);
    }

    private string Process(HttpContext context)
    {
        string result = string.Empty;
        try
        {
            if (context.Request.QueryString["show"] != null)
            {
                switch (context.Request.QueryString["show"].ToString())
                {
                    case "list": result = this.GroupAuthorizationList(context); break;
                }
            }

            if (context.Request.QueryString["type"] != null)
            {
                switch (context.Request.QueryString["type"].ToString())
                {
                    case "update": result = this.UpdateAuthorization(context); break;
                }
            }
        }
        catch (Exception ex)
        {
            //result = "{success: false, message: 'Error found on page! " + ConfigurationManager.AppSettings["ErrorHelpText"].ToString() + "'}";
            //log.Debug("Process() on GroupAuthorizationHandler.ashx.cs: " + ex.Message.ToString());

            Logger.Debug("Process () SysGroupAuthorizationHandler.ashx", ex);
        }
        return result;
    }

    private string GroupAuthorizationList(HttpContext context)
    {
        string result = string.Empty;
        try
        {
            int GroupID = Convert.ToInt32(context.Request.QueryString["gid"].ToString());
            //GroupAuthorizationFacade authorizationFacade = new GroupAuthorizationFacade();
            result = FacadeMenu.ListGroupAuthorizationMenu(GroupID);
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return result;
    }

    private string UpdateAuthorization(HttpContext context)
    {
        string result = string.Empty;
        try
        {
            string Field = context.Request.Params["field"].ToString();
            int ID = Convert.ToInt32(context.Request.Params["id"].ToString());
            int GroupID = Convert.ToInt32(context.Request.Params["groupid"].ToString());
            bool Checked = Convert.ToBoolean(context.Request.Params["checked"].ToString());

            //GroupAuthorizationFacade authorizationFacade = new GroupAuthorizationFacade();
            //GroupAuthorization authorization = new GroupAuthorization();

            SysGroupAuthorization authorization = new SysGroupAuthorization();
            
            //Groups group = new Groups();
            //Menu menu = new Menu();

            SysGroups group = new SysGroups();
            SysMenu menu = new SysMenu();
            
            group.GroupID = GroupID;
            menu.MenuID = ID;

            authorization = Facade.RetrieveAuthorizationByGroupIDAndMenuID(GroupID, ID);
            if (authorization == null)
                authorization = new SysGroupAuthorization();
            authorization.GroupID = GroupID;
            authorization.MenuID = ID;
            

            //if (authorization.ID == 0 && !Checked) authorization.Rowstatus = true;

            switch (Field)
            {
                case "MenuID":
                    //if (Checked) authorization.Rowstatus = false;
                    //else authorization.Rowstatus = true;
                    break;
                case "AllowDisplay": authorization.AllowDisplay = Checked; break;
                case "AllowCreate": authorization.AllowCreate = Checked; break;
                case "AllowUpdate": authorization.AllowUpdate = Checked; break;
                case "AllowDelete": authorization.AllowDelete = Checked; break;
            }

            //authorizationFacade.UpdateAuthorization(authorization);
            authorization.UpdatedBy = base.UserLoggedIn;
            authorization.UpdatedDate = DateTime.Now;
            authorization.CreteadBy = base.UserLoggedIn;
            authorization.CreatedDate = DateTime.Now;
            Facade.UpdateInsert(authorization);
            result = "{\"success\": \"true\"}";
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return result;
    }

 
    public bool IsReusable {
        get {
            return false;
        }
    }

}